import reqests from "../utils/requests.js"
import {STATUS_CODE} from "../utils/variables.js"

/**
 * 用户注册
 * @param {Object} data
 */
export async function registerApi(data) {
	const res = await reqests.request({
		url: "/user/register",
		data,
		method: "POST"
	})
	if(res.code!=STATUS_CODE.SUCCESS_CODE){
		uni.showToast({
			title:"注册失败，请稍后重试",
			icon:'none',
			duration:2000
		})
		return;
	}
	return res;
}

/**
 * 用户登录
 * @param {Object} data
 */
export async function loginApi(data) {
	const res = await reqests.request({
		url: "/user/login",
		data,
		method: "POST"
	})
	if(res.code!=STATUS_CODE.SUCCESS_CODE){
		uni.showToast({
			title:"账号或密码错误",
			icon:'none',
			duration:2000
		})
		return;
	}
	return res.data;
}



/**
 * 微信登录
 */
export function wxLogin(phone) {

	return new Promise((resolve, reject) => {
		// 微信登录
		uni.login({
			"provider": "weixin",
			"onlyAuthorize": true, // 微信登录仅请求授权认证
			success: async function(event) {
				const {code} = event
				//客户端成功获取授权临时票据（code）,向业务服务器发起登录请求。
				const data = {code,phone};
				const res = await reqests.request({
					url: "/user/we_chat_login",
					data,
					method: "POST"
				})
				// 
				resolve(res);
			},
			fail: function(err) {
				// 登录授权失败  
				// err.code是错误码
				reject(err);
			}
		})


	})
}


